package com.jeesite.modules.digitalize.util.response;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @date  16:42:17
 * @author patronli
 * @Description: 通用返回
 */
@Data
public class ResultVo<T> implements Serializable {
    /**
     * 返回的代码，200表示成功，其他表示失败
     */
    private int code;
    /**
     * 成功或失败时返回的错误信息
     */
    private String msg;
    /**
     * 成功时返回的数据信息
     */
    private T data;

    public ResultVo() {
    }

    public ResultVo(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 推荐使用此种方法返回
     *
     * @param resultStatusCode 枚举信息
     * @param data             返回数据
     */
    public ResultVo(ResultStatusCode resultStatusCode, T data) {
        this(resultStatusCode.getCode(), resultStatusCode.getMsg(), data);
    }

    public ResultVo(int code, String msg) {
        this(code, msg, null);
    }

    public ResultVo(ResultStatusCode resultStatusCode) {
        this(resultStatusCode, null);
    }

    public static <T> ResultVo<T> build(int code, String message) {
        return new ResultVo<T>(code, message);
    }

    public static <T> ResultVo<T> success() {
        return new ResultVo<T>(200, "OK");
    }

    public  Boolean isOK() {
        return  this.code == 200;
    }
    public  Boolean isFail() {
        return  this.code != 200;
    }

    public static <T> ResultVo<T> success(String message, T data) {
        return new ResultVo<T>(200, message, data);
    }

    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<T>(200, "OK", data);
    }

    public static <T>  ResultVo<T> failure(String msg) {
        return new ResultVo<T>(500, msg);
    }

    public static <T> ResultVo<T> failure(ResultStatusCode resultStatusCode) {
        return new ResultVo<T>(resultStatusCode.getCode(), resultStatusCode.getMsg());
    }

    public static ResultVo<String> failure(Integer code, String msg) {
        return new ResultVo<String>(code, msg);
    }

    public static ResultVo<Object> failure() {
        return new ResultVo<Object>(500, "内部服务器错误");
    }

    //错误码需要变更时使用，但是应该尽量统一规范错误码
    public static <T> ResultVo<T> failure(int code, String msg, T date) {
        return new ResultVo<T>(code, msg, date);
    }


}
